<template>
	<view class="deliveryAddress">
        <SRV-topbar :title="isOrder ? '选择地址' : '收货地址'" :isLeft="true"></SRV-topbar>
		<view class="line" v-for="(item,index) in pageResult.list" :key="index" @click="selFun(item)">
			<!-- <view v-if="isOrder" :class="selId == item.id ? 'sel' : 'nosel'" @click="selFun(item.id)"></view> -->
			<view class="address" >
				<text>{{item.realname}} — {{item.mobile}}</text>
				<text class="text">{{item.address_name}}{{item.local_name}}{{item.address_details}}</text>
			</view>
			<image :src="'https://hdsq.aoorange.cn/attachment/xcx/icon/bianji.png'" mode="aspectFill" @tap.stop="editAddress($event, index)"></image>
		</view>
		<view v-if="pageResult.list.length == 0" class="nocontent">
			<image src="https://hdsq.aoorange.cn/attachment/xcx/icon/noItem.png" ></image>
			<!-- <text>暂无记录～</text> -->
		</view>
		<view :style="'height:' + bottomHeight + 'rpx;'" :class="isOrder ? 'bottom' : 'bottom' " @click="jump('addOreditAddress', optionsParam)">新增地址</view>
		<!-- <view :style="'height:' + bottomHeight + 'rpx;'" v-if="isOrder" class="bottom" @click="confirmAddress()" style="width:49.5%;margin-left: 51%">确定</view> -->
	</view>
</template>

<script>
	import serve from '../../static/request.js';
	import storage from '../../static/appkey.js';
	import common from '../../static/jump.js';
	export default {
		data() {
			return {
				isOrder: false,
				selId: 0,
				optionsParam: '',
				formType: 0,
				pageParam: {
					appkey: uni.getStorageSync('appkey'),
					'access-token': uni.getStorageSync('access-token'),
					page: 0,
					pageSize: 5
				},
				pageResult: {
					list: [],
					isEmpty: false,
					isEnd: false
				},
				bottomHeight: 110,/* 底部按钮高度，避免iPhonex导航黑条 */
			}
		},
		methods: {
			loadList() {
				var self = this;
				self.pageParam.page++
				serve.request({
						url: '/member/address/index',
						data: self.pageParam
					})
					.then(res => {
						console.log(res.data); 
						if (res.data && res.data.length > 0) {
							self.pageResult.list=self.pageResult.list.concat(res.data);
						}
						self.pageResult.isEmpty = self.pageResult.list.length == 0;
						self.pageResult.isEnd = res.data.length == 0; 
					});
			},
			editAddress(e, key) {
				e.preventDefault();
				let id = this.pageResult.list[key].id;
				common.jump('addOreditAddress', 'id='+id);
			},
			delAddress(key) {
				var self = this;
				let delId = this.pageResult.list[key].id;
				uni.showModal({
				    title: '提示',
				    content: '您确定删除这条收货地址吗',
				    success: function (res) {
				        if (res.confirm) {
				            serve
				            	.request({
				            		url: '/member/address/delete',
				            		data:{
										appkey: uni.getStorageSync('appkey'),
										'access-token': uni.getStorageSync('access-token'),
										id:delId,
									}
				            	})
				            	.then(res => {
									uni.showToast({
										title:res.message,
										icon: 'none',
									});
				            		if(res.code == 200) {
				            			self.pageResult.list.splice(key,1);
				            		} 
				            	});
				        } 
				    }
				});
			},
			confirmAddress() {
				if(this.selId == 0) {
					uni.showToast({
						title: '请选择收货地址',
						icon: 'none',
					});
					return ;
				}
				console.log(this.optionsParam);
				console.log(this.formType);
				if(this.formType == 1) {
					uni.redirectTo({
					    url: '/merchant/storeOrder?'+this.optionsParam+'&address_id='+this.selId
					});
				} else if(this.formType == 3) {
					uni.redirectTo({
					    url: '/merchant/takeOrder?'+this.optionsParam+'&address_id='+this.selId
					});
				}
				
			},
			selFun(dat) {
				if(this.formType == 0) {
					return ;
				}
				var pages = getCurrentPages(); 
				var beforePage = pages[pages.length - 2]; 
				beforePage.$vm.resetAddress(dat);
				uni.navigateBack();
			},
			jump(jump_type, jump_link){
				if(this.formType > 0 && jump_type == "addOreditAddress") { //新增或修改收货地址
					console.info(jump_link);
					uni.redirectTo({
					    url: '/pages/my/newAddress?'+jump_link
					});
					return ;
				}
				common.jump(jump_type, jump_link);
			}
		},
		onLoad(options) {
			if(uni.getSystemInfoSync().safeAreaInsets.top > 20){
				this.bottomHeight = 150;
			}
			this.formType = options.formType || 0;
            console.info(this.formType);
		},
		onShow() {
			this.loadList();
		},
		onPullDownRefresh() {
			console.log('下拉');
			this.pageParam.page=0;
			this.pageResult.list=[];
			this.loadList();
		},
		//触底加载
		onReachBottom() {
			console.log('触底'); 
			if(this.pageResult.isEnd ||this.pageResult.isEmpty){
				return
			}
			this.loadList();
		}
	}
</script>

<style scoped lang="scss">
	.deliveryAddress {
		padding-bottom: 100rpx;

		.bottom {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 32rpx;
			color: rgba(255, 255, 255, 1);
			background: #FFBC00;
			bottom: 0rpx;
			position: fixed;
		}

		.wid50 {
			width: 49.5% !important;
		}

		.line {
			display: flex;
			box-sizing: border-box;
			align-items: flex-start;
			padding: 60rpx 30rpx;
			position: relative;
			border-bottom: 12rpx #F8F8FA solid;

			.sel {
				background-image: url(https://hdsq.aoorange.cn/attachment/xcx/icon/xuanzhong.png);
				background-repeat: no-repeat;
				width: 40rpx;
				height: 40rpx;
				background-size: 100%;
			}

			.nosel {
				width: 36rpx;
				height: 36rpx;
				border: 1rpx solid #999999;
				border-radius: 50%;
			}

			.address {
				font-size: 32rpx;
				display: flex;
				flex-direction: column;
				width: 550rpx;
				margin-left: 23rpx;
				font-weight: bold;
				color: #333333;

				.text {
					margin-top: 32rpx;
					font-weight: 400;
					color: #666666;
				}

			}

			image {
				position: absolute;
				width: 27rpx;
				height: 33rpx;
				top: 140rpx;
				right: 30rpx;
			}
		}
		
		.nocontent{
			width: 100%;
			padding-top: 338rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			image{
				width:220rpx;
				height:220rpx;
			}
			text{
				font-size:28rpx;
				color:rgba(204,204,204,1);
			     margin-top: 30rpx;
			}
		}
	}
</style>
